package com.todod.base.module.infra.controller.admin.basecarousel;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.todod.base.framework.common.pojo.PageParam;
import com.todod.base.framework.common.pojo.PageResult;
import com.todod.base.framework.common.pojo.CommonResult;
import com.todod.base.framework.common.util.object.BeanUtils;
import static com.todod.base.framework.common.pojo.CommonResult.success;

import com.todod.base.framework.excel.core.util.ExcelUtils;

import com.todod.base.framework.apilog.core.annotation.ApiAccessLog;
import static com.todod.base.framework.apilog.core.enums.OperateTypeEnum.*;

import com.todod.base.module.infra.controller.admin.basecarousel.vo.*;
import com.todod.base.module.infra.dal.dataobject.basecarousel.BaseCarouselDO;
import com.todod.base.module.infra.service.basecarousel.BaseCarouselService;

@Tag(name = "管理后台 - 轮播图管理")
@RestController
@RequestMapping("/infra/base-carousel")
@Validated
public class BaseCarouselController {

    @Resource
    private BaseCarouselService baseCarouselService;

    @PostMapping("/create")
    @Operation(summary = "创建轮播图管理")
    @PreAuthorize("@ss.hasPermission('infra:base-carousel:create')")
    public CommonResult<Long> createBaseCarousel(@Valid @RequestBody BaseCarouselSaveReqVO createReqVO) {
        return success(baseCarouselService.createBaseCarousel(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新轮播图管理")
    @PreAuthorize("@ss.hasPermission('infra:base-carousel:update')")
    public CommonResult<Boolean> updateBaseCarousel(@Valid @RequestBody BaseCarouselSaveReqVO updateReqVO) {
        baseCarouselService.updateBaseCarousel(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除轮播图管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('infra:base-carousel:delete')")
    public CommonResult<Boolean> deleteBaseCarousel(@RequestParam("id") Long id) {
        baseCarouselService.deleteBaseCarousel(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得轮播图管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('infra:base-carousel:query')")
    public CommonResult<BaseCarouselRespVO> getBaseCarousel(@RequestParam("id") Long id) {
        BaseCarouselDO baseCarousel = baseCarouselService.getBaseCarousel(id);
        return success(BeanUtils.toBean(baseCarousel, BaseCarouselRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得轮播图管理分页")
    @PreAuthorize("@ss.hasPermission('infra:base-carousel:query')")
    public CommonResult<PageResult<BaseCarouselRespVO>> getBaseCarouselPage(@Valid BaseCarouselPageReqVO pageReqVO) {
        PageResult<BaseCarouselDO> pageResult = baseCarouselService.getBaseCarouselPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BaseCarouselRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出轮播图管理 Excel")
    @PreAuthorize("@ss.hasPermission('infra:base-carousel:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBaseCarouselExcel(@Valid BaseCarouselPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BaseCarouselDO> list = baseCarouselService.getBaseCarouselPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "轮播图管理.xls", "数据", BaseCarouselRespVO.class,
                        BeanUtils.toBean(list, BaseCarouselRespVO.class));
    }

}